﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Service = SLWebApp.ServiceReference_HB;
using System.Collections.ObjectModel;
using SLWebApp.ServiceReference_HB;
using System.Windows.Data;



namespace SLWebApp
{
    public partial class FlightsVessels : UserControl
    {

        #region properties

        /// <summary>
        /// 登陆用户ID
        /// </summary>
        public int UserID
        {
            set;
            get;
        }


        public string UserType
        {
            get
            {
                if (UserID == 1)
                {
                    return "1";
                }
                else
                {
                    return "0";
                }
            }
        }
        #endregion

        #region cstor

        public FlightsVessels()
        {
            InitializeComponent();


            System.Windows.Browser.HtmlDocument doc = System.Windows.Browser.HtmlPage.Document;
            if (doc.QueryString.Count > 0)
            {
                userid = doc.QueryString["userid"].ToString();
            }

            if (!string.IsNullOrEmpty(userid))
            {
                int intResult;
                bool check = Int32.TryParse(userid, out intResult);
                if (check)
                    intUserID = intResult;

                UserID = intResult;
            }


            RegisterEvent();
            InitDate();
        }


        #endregion


        #region public methods

        public void InitDate()
        {
            if (!string.IsNullOrEmpty(userid))
            {

                //获取登陆人的名字
                int intID = 0;
                int.TryParse(userid, out intID);


                string strWhere = string.Empty;
                BeforeLoadData(intID, strWhere);
            }


        }

        #endregion


        #region event handler


        private void cbmSL_first_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
            if (e.AddedItems.Count > 0)
            {
                if (!string.IsNullOrEmpty(userid))
                {
                    //获取登陆人的名字
                    int intID = 0;
                    int.TryParse(userid, out intID);
                    ServiceReference_HB.MainSeaRoutes item = e.AddedItems[0] as ServiceReference_HB.MainSeaRoutes;

                    //绑定服务航线的下拉框
                    if (intID == 1)
                        clientService.GetChildSeaRoutes_ByLineIDAsync(intID, "1", item.ID);
                    else
                        clientService.GetChildSeaRoutes_ByLineIDAsync(intID, "0", item.ID);

                    //获取班期
                }
            }
        }

        void clientService_GetMainSeaRoutes_ByUserIdCompleted(object sender, ServiceReference_HB.GetMainSeaRoutes_ByUserIdCompletedEventArgs e)
        {
            ObservableCollection<ServiceReference_HB.MainSeaRoutes> col = e.Result;
            cbmSL_first.ItemsSource = col;
        }

        void clientService_GetChildSeaRoutes_ByLineIDCompleted(object sender, ServiceReference_HB.GetChildSeaRoutes_ByLineIDCompletedEventArgs e)
        {

            cmbSL_second.ItemsSource = e.Result;
            cmbSL_second.SelectedValuePath = "ID";
        }



        private void btnAdd_VesselSchedule_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (_addUpdateChild == null)
            {
                _addUpdateChild = new ChildWindows.ChildWindow_FlightVessels(this);

            }
            _addUpdateChild.Show();
        }


        /// <summary>
        /// 删除操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBlkDelete_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
           
            ServiceReference_HB.WEB_VM_LINERS  selectedModel= dtgd.SelectedItem as ServiceReference_HB.WEB_VM_LINERS;

            ServiceReference_HB.ServiceForSLClient services = new ServiceForSLClient();
            if (selectedModel == null)
            {
                MessageBox.Show("请选择一行数据！", "提示", MessageBoxButton.OK);
                return;
            }
            else
            {
                
                MessageBoxResult result = MessageBox.Show("您确定删除该数据吗？", "提示", MessageBoxButton.OKCancel);
                if (result.ToString() == "OK")
                {
                    busyIndicator.IsBusy = true;
                    services.IUDForVesselAsync((int)OperationType.Delete, this.UserID, selectedModel);
                    services.IUDForVesselCompleted += (s1, e1) =>
                    {
                        string  outPutStr = e1.Result;
                        if (outPutStr[0] == '0')
                        {

                            MessageBox.Show(outPutStr.Substring(3));
                        }
                        else
                        {
                            PagedCollectionView sources = dtgd.ItemsSource as PagedCollectionView;
                            sources.Remove(selectedModel);

                        }
                        busyIndicator.IsBusy = false;

                    };
                   
                }
               
            }

        }

        private void txtblkUpdate_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ServiceReference_HB.WEB_VM_LINERS selectedModel = dtgd.SelectedItem as ServiceReference_HB.WEB_VM_LINERS;
            if (selectedModel == null)
            {
                MessageBox.Show("请选择一行数据！", "提示", MessageBoxButton.OK);
                return;
            }
            else
            {
                ChildWindows.ChildWindow_FlightVesselsUpdate child = new ChildWindows.ChildWindow_FlightVesselsUpdate(this, selectedModel);
                child.Show();
            }
        }


        /// <summary>
        /// 搜索按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            busyIndicator.IsBusy = true;

            Service.ServiceForSLClient clientServices = new Service.ServiceForSLClient();
             Service.MainSeaRoutes obj = cmbSL_second.SelectedItem as Service.MainSeaRoutes ;
            string slnID = string.Empty;
            string strWhere = string.Empty;
            if (obj != null)
            {

                strWhere = string.Format(" flt_sln_id = {0} ", obj.ID);
            }

            if (UserType == "0")
            {
                clientServices.GetAgentIDByUserIDAsync(this.UserID, "0");
                clientServices.GetAgentIDByUserIDCompleted += (s1, e1) =>
                {
                    if (string.IsNullOrEmpty(strWhere))
                    {
                        strWhere = string.Format("  agent = {0} ", e1.Result);
                    }
                    else
                    {
                        strWhere += string.Format("and  agent = {0} ", e1.Result);
                    }
                    LoadData(strWhere);
                };
            }
            else
            {

                LoadData(strWhere);
            }
        }


        #endregion

        #region private methods

        private void RegisterEvent()
        {

            clientService.GetMainSeaRoutes_ByUserIdCompleted += new EventHandler<ServiceReference_HB.GetMainSeaRoutes_ByUserIdCompletedEventArgs>(clientService_GetMainSeaRoutes_ByUserIdCompleted);


            clientService.GetChildSeaRoutes_ByLineIDCompleted += new EventHandler<ServiceReference_HB.GetChildSeaRoutes_ByLineIDCompletedEventArgs>(clientService_GetChildSeaRoutes_ByLineIDCompleted);
        }



        private void BeforeLoadData(int intID, string strWhere)
        {
            //获取航线名称
            if (intID == 1)//管理员
            {
                clientService.GetMainSeaRoutes_ByUserIdAsync(intID, "1");

                LoadData(strWhere);
            }
            else
            {
           
                clientService.GetMainSeaRoutes_ByUserIdAsync(intID, "0");
                Service.ServiceForSLClient clientServices = new Service.ServiceForSLClient();
                clientServices.GetAgentIDByUserIDAsync(intID, "0");
                clientServices.GetAgentIDByUserIDCompleted += (s, e) =>
                {
                    strWhere += string.Format(" agent = {0} ", e.Result);
                    //string agentID = e.Result;
                    //strWhere = string.Format(strWhere, agentID);
                    LoadData(strWhere);
                };
            }

        }


        /// <summary>
        /// 根据条件绑定值
        /// </summary>
        /// <param name="strWhere"></param>
        private void LoadData(string strWhere)
        {

            Service.ServiceForSLClient clientServices = new Service.ServiceForSLClient();


            clientServices.GetVMLinesAsync(strWhere, " LNR_VSL_CD");

            clientServices.GetVMLinesCompleted += (s, e) =>
            {

                PagedCollectionView pcv = new PagedCollectionView(e.Result);
                pcv.PageSize = 10;

                DataContext = pcv;
                dtgd.ItemsSource = pcv;
                busyIndicator.IsBusy = false;
            };
        }
        #endregion

        #region private fileds

        private string userid = string.Empty;

        int? intUserID = null;

        private Service.ServiceForSLClient clientService = new Service.ServiceForSLClient();


        private ChildWindows.ChildWindow_FlightVessels _addUpdateChild = null;

       

        #endregion
    }

    public enum OperationType
    {
        Add = 1,
        Update = 2,
        Delete = 3

    }
}
